﻿Imports MySql.Data.MySqlClient
Imports MySql.Data.Types
Public Class EMySQLSessionException
    Inherits Exception
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal Message As String)
        MyBase.New(Message)
    End Sub
    Public Sub New(ByVal Message As String, ByVal ex As Exception)
        MyBase.New(Message, ex)
    End Sub
End Class

Public Class TMySession
    Implements System.IDisposable

    Private _conn As MySqlConnection
    Private _UserName As String
    Private _HostName As String
    Private _Password As String
    Private _DatabaseName As String
    Private _Port As Integer = 3306
    Private _Transaction As MySqlTransaction
    Private _Exception As EMySQLSessionException
    Private _LoginTimeOut As Long = 2000
    Public ReadOnly Property Connection() As MySqlConnection
        Get
            Return _conn
        End Get
    End Property
    Public Sub New()
        _conn = New MySqlConnection
    End Sub
    ''' <summary>
    ''' ログインタイムアウトの取得または設定
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property LoginTimeOut() As Long
        Get
            Return _LoginTimeOut
        End Get
        Set(ByVal value As Long)
            _LoginTimeOut = value
        End Set
    End Property
    ''' <summary>
    ''' 最後に発生した例外
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Exception() As EMySQLSessionException
        Get
            Return _Exception
        End Get
    End Property

    ''' <summary>
    ''' ユーザー名の取得または設定
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
        End Set
    End Property

    ''' <summary>
    ''' パスワードの取得または設定
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property

    ''' <summary>
    ''' ホスト名の取得または設定
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property HostName() As String
        Get
            Return _HostName
        End Get
        Set(ByVal value As String)
            _HostName = value
        End Set
    End Property


    ''' <summary>
    ''' データベース名の取得または設定
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DatabaseName() As String
        Get
            Return _DatabaseName
        End Get
        Set(ByVal value As String)
            _DatabaseName = value
        End Set
    End Property

    ''' <summary>
    ''' ポート番号の取得または設定
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Port() As Integer
        Get
            Return _Port
        End Get
        Set(ByVal value As Integer)
            _Port = value
        End Set
    End Property

    ''' <summary>
    ''' 接続状態を取得
    ''' </summary>
    ''' <value></value>
    ''' <returns>接続中：True</returns>
    ''' <remarks></remarks>
    Public Property Connected() As Boolean
        Get
            Return _conn.State = ConnectionState.Executing OrElse _conn.State = ConnectionState.Fetching OrElse _conn.State = ConnectionState.Open
        End Get
        Set(ByVal value As Boolean)
            If value Then
                Open()
            Else
                Close()
            End If
        End Set
    End Property

    ''' <summary>
    ''' 接続文字列の取得
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ConnectionString() As String
        Get
            Return "Data Source=" & Me.HostName & ";database=" & Me.DatabaseName & ";User Id=" & Me.UserName & ";Password=" & Me.Password & ";Port=" & Me.Port.ToString
        End Get
    End Property

    ''' <summary>
    ''' 接続
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Open()
        Try
            _conn.ConnectionString = Me.ConnectionString
            _conn.Open()
            _Transaction = _conn.BeginTransaction
        Catch ex As Exception
            Throw New EMySQLSessionException(ex.Message, ex)
        End Try
    End Sub

    ''' <summary>
    ''' 切断
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Close()
        Try
            _conn.Close()
        Catch ex As Exception
            Throw New EMySQLSessionException(ex.Message, ex)
        End Try
    End Sub

    Public Sub Commit()
        Try
            _Transaction.Commit()
        Catch ex As Exception
            Throw New EMySQLSessionException(ex.Message, ex)
        End Try
    End Sub

    Public Sub Rollback()
        Try
            _Transaction.Rollback()
        Catch ex As Exception
            Throw New EMySQLSessionException(ex.Message, ex)
        End Try
    End Sub

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If

            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
